package com.sharemarking.wa.platform;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import com.sharemarking.wa.platform.mapper.MemberMapper;
import com.sharemarking.wa.platform.properties.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableCaching
@EnableAsync
@MapperScan(basePackageClasses= MemberMapper.class)
@EnableConfigurationProperties({WechatProperties.class, AliPayProperties.class,WechatProperties.class, CosProperties.class})
public class PlatformApplication {

    @Autowired
    private WechatProperties wrop;

    @Autowired
    private CosProperties cop;

    public static void main(String[] args) {
        SpringApplication.run(PlatformApplication.class, args);
    }


    @Bean
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wrop.getAppId());
        payConfig.setMchId(wrop.getMchId());
        payConfig.setMchKey(wrop.getMchKey());
        payConfig.setKeyPath(wrop.getKeyPath());
        WxPayService payService = new WxPayServiceImpl();
        payService.setConfig(payConfig);
        return payService;
    }

    @Bean
    public COSClient cosClient(){
        COSCredentials cred = new BasicCOSCredentials(cop.getSecretid(), cop.getSecretkey());
        Region region = new Region(cop.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }
}
